package pl.PawelAndAlicja.badugiproject.main.Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Server{  // singleton

   	public static Server getInstance(/*String name, int smallBlindInt, int bigBlindInt*/) {
       /* tableName = name;
        smallBlindAmounts = smallBlindInt;
        bigBlindAmounts = bigBlindInt;*/
        return SingletonHolder.server;
    }
    
    public void listenSocket() {
        try {
        	client = serverSocket.accept();
        	System.out.println("Polaczono z klientem");
        } 
        catch (IOException e) {
        	System.out.println("Accept failed: 345"); System.exit(-1);
        }
      }

    public static boolean isServerReady() {
        try
        {
            new Socket("localhost", 345);
            System.out.println("GOOD: Server works");
            return true;

        }
        catch (Exception e)
        {
            System.out.println("ERROR: Server down");
            return false;
        }
    }

    public static String getTableName() {
        return tableName;
    }

    public static int getSmallBlindAmounts() {
        return smallBlindAmounts;
    }

    public static int getBigBlindAmounts() {
        return bigBlindAmounts;
    }

    public static void main(String[] args) {
        Server serverOut = Server.getInstance(/*"Fajny serwer", 100, 200*/);
        serverOut.listenSocket();

    }

    protected void finalize() {
        try {
            client.close();
            serverSocket.close();
        }
        catch (IOException e) {
            System.out.println("Could not close.");
            System.exit(-1);
        }
    }

    private Server() {
        try {
            serverSocket = new ServerSocket(345);
            System.out.println("Utworzono serwer 345");
//            System.exit(-1);
        }
        catch (IOException e) {
            System.out.println("Server is creating or cannot listen on port 345");
//            System.exit(-1);
        }
    }

    private static class SingletonHolder{
        private final static Server server = new Server();
    }

    private static volatile Server server = null;
    private static ServerSocket serverSocket = null;
    private static Socket client = null;

    private static String tableName = null;

    public static void setBigBlindAmounts(int bigBlindAmounts) {
        Server.bigBlindAmounts = bigBlindAmounts;
    }

    public static void setSmallBlindAmounts(int smallBlindAmounts) {
        Server.smallBlindAmounts = smallBlindAmounts;
    }

    public static void setTableName(String tableName) {
        Server.tableName = tableName;
    }

    private static int smallBlindAmounts = 0;
    private static int bigBlindAmounts = 0;
}